﻿using System;
using System.Collections.Generic;

namespace KKCai.HNKL.Utility
{
    internal class Utils
    {
        public const string FORMAT_N1 = "{0:D2}";
        public const string FORMAT_N2 = "{0:D2} {1:D2}";
        public const string FORMAT_N3 = "{0:D2} {1:D2} {2:D2}";
        public const string FORMAT_N4 = "{0:D2} {1:D2} {2:D2} {3:D2}";
        public const string FORMAT_N5 = "{0:D2} {1:D2} {2:D2} {3:D2} {4:D2}";
        public const string FORMAT_N6 = "{0:D2} {1:D2} {2:D2} {3:D2} {4:D2} {5:D2}";
        public const string FORMAT_N7 = "{0:D2} {1:D2} {2:D2} {3:D2} {4:D2} {5:D2} {6:D2}";
        public const string FORMAT_N8 = "{0:D2} {1:D2} {2:D2} {3:D2} {4:D2} {5:D2} {6:D2} {7:D2}";


        /// <summary>
        /// 连二直(7种组合)
        /// </summary>
        public static List<string> GetA2(string n8)
        {
            List<string> list = new List<string>(7);

            string[] items = n8.Split(' ');

            list.Add(String.Format(FORMAT_N2, items[0], items[1]));
            list.Add(String.Format(FORMAT_N2, items[1], items[2]));
            list.Add(String.Format(FORMAT_N2, items[2], items[3]));
            list.Add(String.Format(FORMAT_N2, items[3], items[4]));
            list.Add(String.Format(FORMAT_N2, items[4], items[5]));
            list.Add(String.Format(FORMAT_N2, items[5], items[6]));
            list.Add(String.Format(FORMAT_N2, items[6], items[7]));

            return list;
        }

        /// <summary>
        /// 连二组(14种组合)
        /// </summary>
        public static List<string> GetP2(string n8)
        {
            List<string> list = new List<string>(14);

            string[] items = n8.Split(' ');

            list.Add(String.Format(FORMAT_N2, items[0], items[1]));
            list.Add(String.Format(FORMAT_N2, items[1], items[2]));
            list.Add(String.Format(FORMAT_N2, items[2], items[3]));
            list.Add(String.Format(FORMAT_N2, items[3], items[4]));
            list.Add(String.Format(FORMAT_N2, items[4], items[5]));
            list.Add(String.Format(FORMAT_N2, items[5], items[6]));
            list.Add(String.Format(FORMAT_N2, items[6], items[7]));

            list.Add(String.Format(FORMAT_N2, items[1], items[0]));
            list.Add(String.Format(FORMAT_N2, items[2], items[1]));
            list.Add(String.Format(FORMAT_N2, items[3], items[2]));
            list.Add(String.Format(FORMAT_N2, items[4], items[3]));
            list.Add(String.Format(FORMAT_N2, items[5], items[4]));
            list.Add(String.Format(FORMAT_N2, items[6], items[5]));
            list.Add(String.Format(FORMAT_N2, items[7], items[6]));

            return list;
        }

        /// <summary>
        /// 8选2(28种组合)
        /// </summary>
        public static List<string> Get8P2(string n8)
        {
            string[] items = n8.Split(' ');

            List<string> list = new List<string>(28);

            for (int i = 0; i <= 6; i++)
            {
                for (int j = i + 1; j <= 7; j++)
                {
                    list.Add(String.Format(FORMAT_N2, items[i], items[j]));
                }
            }

            return list;
        }

        /// <summary>
        /// 8选3(56种组合)
        /// </summary>
        public static List<string> Get8P3(string n8)
        {
            string[] items = n8.Split(' ');

            List<string> list = new List<string>(56);

            for (int i = 0; i <= 5; i++)
            {
                for (int j = i + 1; j <= 6; j++)
                {
                    for (int k = j + 1; k <= 7; k++)
                    {
                        list.Add(String.Format(FORMAT_N3, items[i], items[j], items[k]));
                    }
                }
            }

            return list;
        }

        /// <summary>
        /// 8选4(70种组合)
        /// </summary>
        public static List<string> Get8P4(string n8)
        {
            string[] items = n8.Split(' ');

            List<string> list = new List<string>(70);

            for (int i = 0; i <= 4; i++)
            {
                for (int j = i + 1; j <= 5; j++)
                {
                    for (int k = j + 1; k <= 6; k++)
                    {
                        for (int m = k + 1; m <= 7; m++)
                        {
                            list.Add(String.Format(FORMAT_N4, items[i], items[j], items[k], items[m]));
                        }
                    }
                }
            }

            return list;
        }

        /// <summary>
        /// 8选5(56种组合)
        /// </summary>
        public static List<string> Get8P5(string n8)
        {
            string[] items = n8.Split(' ');

            List<string> list = new List<string>(56);

            for (int i = 0; i <= 3; i++)
            {
                for (int j = i + 1; j <= 4; j++)
                {
                    for (int k = j + 1; k <= 5; k++)
                    {
                        for (int m = k + 1; m <= 6; m++)
                        {
                            for (int n = m + 1; n <= 7; n++)
                            {
                                list.Add(String.Format(FORMAT_N5, items[i], items[j], items[k], items[m], items[n]));
                            }
                        }
                    }
                }
            }

            return list;
        }


        public static string Sort(string result)
        {
            object[] array = Array.ConvertAll(result.Split(' '), value => (object)value);

            Array.Sort<object>(array);

            switch (result.Split(' ').Length)
            {
                case 1: return String.Format(FORMAT_N1, array);
                case 2: return String.Format(FORMAT_N2, array);
                case 3: return String.Format(FORMAT_N3, array);
                case 4: return String.Format(FORMAT_N4, array);
                case 5: return String.Format(FORMAT_N5, array);
                case 6: return String.Format(FORMAT_N6, array);
                case 7: return String.Format(FORMAT_N7, array);
                case 8: return String.Format(FORMAT_N8, array);
                default: return result;
            }
        }

    }
}
